package com.manager.ctroller;

import com.example.domain.User;
import com.example.utils.Utils;
import com.manager.dao.UserDao;
import com.manager.domain.Person;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.List;

/**
 * @author mucd
 * 2021年06月25日  15:48
 */
@WebServlet(value = "/showAll")
public class ShowAll extends HttpServlet implements UserDao  {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //将查询到的数据设置到作用域
        session.setAttribute("setPer",showAll());
        session.setAttribute("setDataSize",showAll().size());
        System.out.println(showAll().size());
        resp.sendRedirect("manager.jsp");
    }

    @Override
    public List<Person> showAll() {
        QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());
        try {
            String sql = "select * from manager.person limit ?,? ";
            return queryRunner.query(
                    sql
                    , new BeanListHandler<>(Person.class)
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
